import { defineComponent, inject, ref, type Ref } from 'vue';
<%_ if (enableTranslation) { _%>
import { useI18n } from 'vue-i18n';
<%_ } _%>
import { useRoute, useRouter } from 'vue-router';

<%_ if (anyFieldIsBlobDerived) { _%>
import useDataUtils from '@/shared/data/data-utils.service';
<%_ } _%>
<%_ if (anyFieldIsDuration || anyFieldIsTimeDerived) { _%>
import { useDateFormat } from '@/shared/composables'
<%_ } _%>
import { type I<%= entityAngularName %> } from '@/shared/model/<%= entityModelFileName %>.model';
import <%= entityAngularName %>Service from './<%= entityFileName %>.service';
import { useAlertService } from '@/shared/alert/alert.service';

export default defineComponent({
  name: '<%= entityAngularName %>Details',
  setup() {
<%_ if (anyFieldIsDuration || anyFieldIsTimeDerived) { _%>
    const dateFormat = useDateFormat();
<%_ } _%>
    const <%= entityInstance %>Service = inject('<%= entityInstance %>Service', () => new <%= entityAngularName %>Service());
    const alertService = inject('alertService', () => useAlertService(), true);

<% if (anyFieldIsBlobDerived) { %>
    const dataUtils = useDataUtils();
<% } %>
    const route = useRoute();
    const router = useRouter();

    const previousState = () => router.go(-1);
    const <%= entityInstance %>: Ref<I<%= entityAngularName %>> = ref({});

    const retrieve<%= entityAngularName %> = async <%= entityInstance %><%= primaryKey.nameCapitalized %> => {
      try {
        const res = await <%= entityInstance %>Service().find(<%= entityInstance %><%= primaryKey.nameCapitalized %>);
        <%= entityInstance %>.value = res;
      } catch (error) {
        alertService.showHttpError(error.response);
      }
    };

    if (route.params?.<%= entityInstance %>Id) {
      retrieve<%= entityAngularName %>(route.params.<%= entityInstance %>Id);
    }

    return {
<%_ if (anyFieldIsDuration || anyFieldIsTimeDerived) { _%>
      ...dateFormat,
<%_ } _%>
      alertService,
      <%= entityInstance %>,
<% if (anyFieldIsBlobDerived) { %>
      ...dataUtils,
<% } %>
      previousState,
<%_ if (enableTranslation) { _%>
      t$: useI18n().t,
<%_ } _%>
    };
  },
});
